home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 #2 / Ham Radio 2000 - Volume 2.iso / HAMV2 / TCP_IP / TNOS230S / MBFILTER.C < prev    next >
Encoding:
C/C++ Source or Header  |  1997-08-18  |  4.7 KB  |  205 lines

  1. #ifndef _lint
  2. #include <unistd.h>
  3. #endif
  4. #include "global.h"
  5. #include "commands.h"
  6. #ifndef MSDOS
  7. #include <time.h>
  8. #endif
  9. #include "mailbox.h"
  10. #include "files.h"
  11.  
  12.  
  13. #if !defined(_lint)
  14. static char rcsid[] OPTIONAL = "$Id: mbfilter.c,v 1.16 1997/08/19 01:19:22 root Exp root $";
  15. #endif
  16.  
  17. #ifdef MAILFILTER
  18.  
  19. extern char *reqsvr_filter_subject;
  20. extern int reqsvr_bypass_permissions;
  21. static char *makeone (char *buf);
  22.  
  23.  
  24. #define LOOKING  0
  25. #define PARSING  1
  26. #define COMPLETE 2
  27.  
  28. static char *
  29. makeone (buf)
  30. char *buf;
  31. {
  32. char *temp;
  33.  
  34.     temp = strchr(buf, ':');
  35.     if (temp)    {
  36.         *temp++ = 0;
  37.         return (temp);
  38.     } else
  39.         return (NULLCHAR);
  40. }
  41.  
  42.  
  43.  
  44. int
  45. mbfilter(fp,from, to)
  46. FILE *fp;
  47. const char *from;
  48. const char *to OPTIONAL;
  49. {
  50. char subject[256], realfrom[128], realto[238], bid[128];
  51. long startat;
  52. int retval = 1, k;
  53. FILE *fltfp;
  54. char buf[256];
  55. int state;
  56. char *filename = NULLCHAR, *dirname = NULLCHAR;
  57. char *temp, *cp, *temp2;
  58. int override = 0;
  59.  
  60.     parseheader (fp, realfrom, subject, realto, bid, buf, &startat);
  61.     state = LOOKING;
  62.     fltfp = fopen (FilterFile, "r");
  63.     if (fltfp)    {
  64.         while (!feof (fltfp))    {
  65.             if (fgets(buf, 128, fltfp) == NULLCHAR)
  66.                 break;
  67.             if (feof(fltfp))
  68.                 continue;
  69.             rip(buf);
  70.             if (*buf == '#' || *buf == '\n')
  71.                 continue;
  72.             if (state == LOOKING)    {
  73.                 if (*buf != ':')
  74.                     continue;
  75.                 /* check for match */
  76.                 /* check the from user */
  77.                 cp = &buf[1];
  78.                 temp = makeone (cp);
  79.                 if (*cp && *cp != '*' && strnicmp (cp, realfrom, strlen(cp)))
  80.                     continue;
  81.                 cp = temp;
  82.  
  83.                 /* check the from bbs */
  84.                 temp = makeone (cp);
  85.                 temp2 = strchr (realfrom, '@');
  86.                 if (temp2)
  87.                     temp2++;
  88.                 if (*cp && *cp != '*' && (temp2 && strnicmp (cp, temp2, strlen(cp))))
  89.                     continue;
  90.                 cp = temp;
  91.  
  92.                 /* check the to user */
  93.                 temp = makeone (cp);
  94.                 if (temp2)
  95.                     temp2++;
  96.                 if (*cp && *cp != '*' && strnicmp (cp, realto, strlen(cp)))
  97.                     continue;
  98.                 cp = temp;
  99.                 
  100.                 /* check the to bbs */
  101.                 temp = makeone (cp);
  102.                 temp2 = strchr (realto, '@');
  103.                 if (temp2)
  104.                     temp2++;
  105.                 if (*cp && *cp != '*' && (temp2 && strnicmp (cp, temp2, strlen(cp))))
  106.                     continue;
  107.                 cp = temp;
  108.  
  109.                 /* check the bid */
  110.                 temp = makeone (cp);
  111.                 temp2 = strchr (bid, '@');
  112.                 if (temp2)
  113.                     *temp2++ = 0;
  114.                 if (*cp && *cp != '*' && strnicmp (cp, bid, strlen(cp)))
  115.                     continue;
  116.                 cp = temp;
  117.  
  118.                 /* check the subject */
  119.                 if ((temp = strchr(cp, ':')) != NULLCHAR)
  120.                     *temp = 0;
  121.                 if (*cp && *cp != '*' && strstr(subject, cp) == NULLCHAR)
  122.                     continue;
  123.  
  124.                 /* if found */
  125.                 state = PARSING;
  126.             } else if (state == PARSING)    {
  127.                 if (*buf == ':')
  128.                     state = COMPLETE;
  129.                 else if (*buf == '@')
  130.                     dirname = strdup(skipwhite(&buf[1]));
  131.                 else if (*buf == '=')
  132.                     retval = atoi(skipwhite(&buf[1]));
  133.                 else if (*buf == '!')
  134.                     override = 1;
  135.                 else if (*buf == '~')    {
  136.                     temp = skipwhite (&buf[1]);
  137.                     if (*temp != ':')
  138.                         filename = strdup (temp);
  139.                     else    {
  140.                         if (!strnicmp (&temp[1], "bid", 3))
  141.                             filename = strdup (bid);
  142.                         else if (!strnicmp (&temp[1], "subject", 7))    {
  143.                             k = atoi (&temp[9]);
  144.                             temp = subject;
  145.                             while (k--)    {
  146.                                 temp = skipnonwhite (temp);
  147.                                 temp = skipwhite(temp);
  148.                             }
  149.                             filename = strdup (temp);
  150.                         } else if (!strnicmp (&temp[1], "temp", 4))    {
  151.                             strncpy (buf, &temp[6], 2);
  152.                             strncpy (&buf[2], "XXXXXX", 254);
  153.                             filename = strdup ((char *)mktemp (buf));
  154.                         }
  155.                         if (filename)    {
  156.                             temp = strchr (filename, '@');
  157.                             if (temp)
  158.                                 *temp = 0;
  159.                             if (strlen(filename) > 12)
  160.                                 filename[12] = 0;
  161.                         }
  162.                     }
  163.                     if (filename)
  164.                         (void) strlwr (filename);
  165.                 }
  166.             }
  167.             if (state == COMPLETE)
  168.                 break;
  169.         }
  170.         if ((state != LOOKING) && (filename != NULLCHAR))     {
  171.             if (dirname != NULLCHAR)    {
  172.                 temp = &dirname[strlen(dirname) - 1];
  173.                 if (*temp == '/' || *temp == '\\')
  174.                     *temp = 0;
  175.                 temp2 = strrchr (filename, '\\');
  176.                 if (!temp2)
  177.                     temp2 = strrchr (filename, '/');
  178.                 if (temp2)
  179.                     temp2++;
  180.                 else
  181.                     temp2 = filename;
  182.             } else
  183.                 temp2 = filename;
  184.  
  185.             sprintf (buf, "uploadblind %s%s%s", (dirname) ? dirname : "", (dirname) ? "/" : "", temp2);
  186.             log(-1, "mbfilter: '%s' received", &buf[12]);
  187. #if 1
  188.             reqsvr_bypass_permissions = override;
  189.             reqsvr_filter_subject = buf;        /*lint !e789 */
  190.             (void) reqsvr (fp, from);
  191. #endif
  192.         }
  193.         if (!retval)
  194.             log(-1, "mbfilter: removed message from: %s, to: %s, bid: %s, subj: %s", realfrom, realto, bid, subject);
  195.         (void) fclose (fltfp);
  196.     }
  197.     if (dirname != NULLCHAR)
  198.         free (dirname);
  199.     if (filename != NULLCHAR)
  200.         free (filename);
  201.     return (retval);
  202. }
  203.  
  204. #endif /* MAILFILTER */
  205.